package com.community.body.service.impl;

import java.util.List;

import com.community.body.exception.CustomException;
import com.community.body.domain.ZyRoom;
import com.community.body.mapper.ZyRoomMapper;
import com.community.body.service.IZyRoomService;
import com.community.common.utils.DateUtils;
import com.community.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * 房屋信息Service业务层处理
 * 
 * @author community
 * @date 2024-04-09
 */
@Service
public class ZyRoomServiceImpl implements IZyRoomService
{
    @Resource
    private ZyRoomMapper zyRoomMapper;

    private static final Logger log = LoggerFactory.getLogger(ZyRoomServiceImpl.class);

    /**
     * 查询房屋信息
     * 
     * @param roomId 房屋信息主键
     * @return 房屋信息
     */
    @Override
    public ZyRoom selectZyRoomByRoomId(Long roomId)
    {
        return zyRoomMapper.selectZyRoomByRoomId(roomId);
    }

    /**
     * 查询房屋信息列表
     * 
     * @param zyRoom 房屋信息
     * @return 房屋信息
     */
    @Override
    public List<ZyRoom> selectZyRoomList(ZyRoom zyRoom)
    {
        return zyRoomMapper.selectZyRoomList(zyRoom);
    }

    /**
     * 新增房屋信息
     * 
     * @param zyRoom 房屋信息
     * @return 结果
     */
    @Override
    public int insertZyRoom(ZyRoom zyRoom)
    {
        // 设置创建时间
        zyRoom.setCreateTime(DateUtils.getNowDate());
        // 时间戳
        zyRoom.setRoomCode("Room_" + System.currentTimeMillis());
        return zyRoomMapper.insertZyRoom(zyRoom);
    }

    /**
     * 修改房屋信息
     * 
     * @param zyRoom 房屋信息
     * @return 结果
     */
    @Override
    public int updateZyRoom(ZyRoom zyRoom)
    {
        zyRoom.setUpdateTime(DateUtils.getNowDate());
        return zyRoomMapper.updateZyRoom(zyRoom);
    }

    /**
     * 批量删除房屋信息
     * 
     * @param roomIds 需要删除的房屋信息主键
     * @return 结果
     */
    @Override
    public int deleteZyRoomByRoomIds(Long[] roomIds)
    {
        return zyRoomMapper.deleteZyRoomByRoomIds(roomIds);
    }

    @Override
    public List<Object> selectZyRoomCascade( ) {
        return zyRoomMapper.selectZyRoomCascade();
    }

    /**
     * 导入
     * @param dataManagementList
     * @param isUpdateSupport
     * @return
     */
    @Override
    public String importData(List<ZyRoom> dataManagementList, Boolean isUpdateSupport) {
        if (StringUtils.isNull(dataManagementList) || dataManagementList.size() == 0)
        {
            throw new CustomException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        List<ZyRoom> existList = selectZyRoomList(null);
        for (ZyRoom importData : dataManagementList)
        {
            try {
                //验证是否存在这个信息
                boolean userFlag = false;
                for (ZyRoom entry : existList) {
                    if (entry.getCommunityId().equals(importData.getCommunityId()) && entry.getRoomName().equals(importData.getRoomName())) {
                        userFlag = true;
                        break;
                    }
                }
                if (!userFlag) {
                    insertZyRoom(importData);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据 " + importData.getRoomName() + " 导入成功");
                } else if (isUpdateSupport) {
                    updateZyRoom(importData);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据 " + importData.getRoomName() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、数据 " + importData.getRoomName() + " 已存在");
                }
            }catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、数据 " + importData.getRoomName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public List<ZyRoom> selectZyRoomExport(Long[] arr) {
        return zyRoomMapper.selectZyRoomExport(arr);
    }

    @Override
    public int selectCheck(ZyRoom zyRoom) {
        return zyRoomMapper.selectCheck(zyRoom);
    }

    /**
     * 查询可供选择的房间
     * @param id
     * @return
     */
    @Override
    public List<ZyRoom> selectBindingRoomList(String id) {
        return zyRoomMapper.selectRoomList(id);
    }

}
